package cn.itcast.bos.service.trantis.Impl;

import cn.itcast.bos.Dao.take_delicery.WayBillDao;
import cn.itcast.bos.Dao.trantis.TrantisInfoRepostiory;
import cn.itcast.bos.domain.take_delivery.WayBill;
import cn.itcast.bos.domain.transit.TransitInfo;
import cn.itcast.bos.index.WayBillIndexRepository;
import cn.itcast.bos.service.trantis.TrantisInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class TrantisInfoServiceImpl implements TrantisInfoService {
    @Autowired
    private WayBillDao wayBillDao;
    @Autowired
    private TrantisInfoRepostiory trantisInfoRepostiory;
    @Autowired
    private WayBillIndexRepository wayBillIndexRepository;
    @Override
    public void createTransit(String wayBillIds) {
        //创建Transit,对象并保存,修改运单状态为已发货
        if(StringUtils.isNotBlank(wayBillIds)){
            for(String id:wayBillIds.split(",")){
                WayBill wayBill = wayBillDao.findOne(Integer.parseInt(id));
                //生成对象
                TransitInfo transitInfo = new TransitInfo();
                transitInfo.setWayBill(wayBill);
                transitInfo.setStatus("出入库中转");
                //保存
                trantisInfoRepostiory.save(transitInfo);
                //修改运单状态为已发货
                wayBill.setSignStatus(2);
                //更新elasticSearch
                wayBillIndexRepository.save(wayBill);
            }
        }
    }

    @Override
    public Page<TransitInfo> pageQuery(Pageable pageable) {
        return trantisInfoRepostiory.findAll(pageable);
    }
}
